// OutPutList.cpp : implementation file
//

#include "stdafx.h"
#include "waveeditor.h"
#include "waveeditorDoc.h"
#include "OutPutList.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// COutPutList

IMPLEMENT_DYNCREATE(DebugViewList, CListView)

DebugViewList::DebugViewList():clist(GetListCtrl())
{
}

DebugViewList::~DebugViewList()
{
}


BEGIN_MESSAGE_MAP(DebugViewList, CListView)
	//{{AFX_MSG_MAP(COutPutList)
	ON_NOTIFY_REFLECT(NM_KILLFOCUS, OnKillfocus)
	//}}AFX_MSG_MAP
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COutPutList drawing

void DebugViewList::OnDraw(CDC* pDC)
{
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// COutPutList diagnostics

#ifdef _DEBUG
void DebugViewList::AssertValid() const
{
	CListView::AssertValid();
}

void DebugViewList::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

ZWaveeditorDoc* DebugViewList::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(ZWaveeditorDoc)));
	return (ZWaveeditorDoc*)m_pDocument;
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// COutPutList message handlers


void DebugViewList::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	ChangeWindowTitle();
	MakeNewList();	
	
}

void DebugViewList::MakeNewList()
{
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	clist.DeleteAllItems();
	for(int i=0;i<pDoc->StatusList.GetNumberOfMessages();i++)
	{
		clist.InsertItem(i,pDoc->StatusList.GetMessage(i));
	}
}

BOOL DebugViewList::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	cs.style	|=	LVS_SHOWSELALWAYS | LVS_REPORT | LVS_SINGLESEL|LVS_EDITLABELS;
	return CListView::PreCreateWindow(cs);
}

void DebugViewList::OnInitialUpdate() 
{
	clist.InsertColumn(0,"MESSAGES",LVCFMT_CENTER,600,0);
//	clist.InsertColumn(1,"Line Number",LVCFMT_CENTER,200,0);
	CListView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	
}

void DebugViewList::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	// TODO: Add your specialized code here and/or call the base class
 	ChangeWindowTitle();
	CListView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}

void DebugViewList::OnKillfocus(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	ChangeWindowTitle();
	*pResult = 0;
}


void DebugViewList::ChangeWindowTitle()
{
	CFrameWnd* pFrame	=	GetParentFrame();
	ZWaveeditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CString	name	=	"Debug OutPut View (";
	name			+=	pDoc->GetTitle()+ ")";
	pFrame->SetWindowText(name);

}

void DebugViewList::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
	// TODO: Add your control notification handler code here
    // Take the default processing unless we set this to something else below.
    *pResult = CDRF_DODEFAULT;

    // First thing - check the draw stage. If it's the control's prepaint
    // stage, then tell Windows we want messages for every item.

    if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
        {
        *pResult = CDRF_NOTIFYITEMDRAW;
        }
    else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
        {
        // This is the prepaint stage for an item. Here's where we set the
        // item's text color. Our return value will tell Windows to draw the
        // item itself, but it will use the new color we set here.
        // We'll cycle the colors through red, green, and light blue.

        COLORREF crText;
		CString line	=	clist.GetItemText(pLVCD->nmcd.dwItemSpec,0);
		crText			=	RGB(0,0,0);
		if(line.Find("ERROR") >=	0)
		{
			crText	=	RGB(255,0,0);
		}
		if(line.Find("WARNING") >=	0)
		{
			crText	=	RGB(200,200,0);
		}
        // Store the color back in the NMLVCUSTOMDRAW struct.
        pLVCD->clrText = crText;

        // Tell Windows to paint the control itself.
        *pResult = CDRF_DODEFAULT;
        }
}
